/**
 * Copyright (c) 2021 OceanBase
 * OceanBase CE is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */

#ifndef OCEANBASE_SQL_ENGINE_OB_FRAME_INFO_
#define OCEANBASE_SQL_ENGINE_OB_FRAME_INFO_
#include "lib/utility/ob_print_utils.h"
#include "lib/allocator/ob_allocator.h"
#include "lib/container/ob_fixed_array.h"
#include "sql/engine/expr/ob_expr.h"

namespace oceanbase {
namespace sql {
class ObPhysicalPlanCtx;

struct ObFrameInfo {
  OB_UNIS_VERSION_V(1);

  public:
  ObFrameInfo() : expr_cnt_(0), frame_idx_(0), frame_size_(0)
  {}

  ObFrameInfo(uint64_t expr_cnt, uint32_t frame_idx, uint64_t frame_size)
      : expr_cnt_(expr_cnt), frame_idx_(frame_idx), frame_size_(frame_size)
  {}

  TO_STRING_KV(K_(expr_cnt), K_(frame_idx), K_(frame_size));

  public:
  uint64_t expr_cnt_;
  uint32_t frame_idx_;
  uint64_t frame_size_;
};

struct ObExprFrameInfo {
  static const int64_t EXPR_CNT_PER_FRAME = common::MAX_FRAME_SIZE / (sizeof(ObDatum) + sizeof(ObEvalInfo));
  ObExprFrameInfo(common::ObIAllocator& allocator)
      : need_ctx_cnt_(0),
        rt_exprs_(0, common::ModulePageAllocator(allocator)),
        const_frame_ptrs_(allocator),
        const_frame_(allocator),
        param_frame_(allocator),
        dynamic_frame_(allocator),
        datum_frame_(allocator)
  {}

  // pre allocate memory execution need, including frame memory and expr_ctx memory.
  // @param exec_ctx is execution context, we need to init its member: frames_, frame_cnt_, expr_ctx_arr_
  int pre_alloc_exec_memory(ObExecContext& exec_ctx) const;

  int alloc_frame(
      common::ObIAllocator& exec_allocator, ObPhysicalPlanCtx& phy_ctx, uint64_t& frame_cnt, char**& frames) const;

  TO_STRING_KV(K_(const_frame_ptrs), K_(const_frame), K_(dynamic_frame), K_(datum_frame));

  public:
  // count of expression need context. exec_ctx pre allocate memory according to it.
  int64_t need_ctx_cnt_;
  // all physical expressions generated by expe_code_generator
  common::ObArray<ObExpr> rt_exprs_;
  common::ObFixedArray<char*, common::ObIAllocator> const_frame_ptrs_;
  common::ObFixedArray<ObFrameInfo, common::ObIAllocator> const_frame_;
  common::ObFixedArray<ObFrameInfo, common::ObIAllocator> param_frame_;
  common::ObFixedArray<ObFrameInfo, common::ObIAllocator> dynamic_frame_;
  common::ObFixedArray<ObFrameInfo, common::ObIAllocator> datum_frame_;
};

struct ObPreCalcExprFrameInfo : public ObExprFrameInfo, public common::ObDLinkBase<ObPreCalcExprFrameInfo> {
  public:
  ObPreCalcExprFrameInfo(common::ObIAllocator& allocator) : ObExprFrameInfo(allocator), pre_calc_rt_exprs_(allocator)
  {}

  int assign(const ObPreCalcExprFrameInfo& other, common::ObIAllocator& allocator);

  public:
  common::ObFixedArray<ObExpr*, common::ObIAllocator> pre_calc_rt_exprs_;
};
}  // end namespace sql
}  // end namespace oceanbase
#endif /* OCEANBASE_SQL_ENGINE_OB_FRAME_INFO_*/
